package com.yanqu.road.server.manager.activity.cat;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cat.data.CatDrawNotice;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.CatProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossCatActivityMgr extends TempMgr {

    private static Map<Integer, CatActivity> activityMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        if (activityMap == null) {
            activityMap = new ConcurrentHashMap<>();
        }
        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.Cat.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (!activityMap.containsKey(activityInfo.getActivityId())) {
                getCatActivity(activityInfo.getActivityId());
            }
        }

        return true;
    }

    public static CatActivity getCatActivity(int activityId) {
        if (activityMap.get(activityId) == null) {
            synchronized (activityMap) {
                if (activityMap.get(activityId) == null) {
                    // 读配置 获取活动信息 保证有活动信息
                    ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    CatActivity activity = new CatActivity(activityInfo);
                    activity.init();
                    activityMap.put(activityId, activity);
                }
            }
        }
        return activityMap.get(activityId);
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        if (activityMap != null) {
            for (CatActivity activity : activityMap.values()) {
                activity.save();
            }
        }
        return true;
    }


    public static CatProto.CatNoticeMsg.Builder parseNoticeMsg(CatDrawNotice notice) {
        CatProto.CatNoticeMsg.Builder noticeMsg = CatProto.CatNoticeMsg.newBuilder();
        noticeMsg.setUserId(notice.getUserId());
        noticeMsg.setGoodsId(notice.getRewardId());
        noticeMsg.setPlayerMsg(PlayerBasePb.parsePlayerBaseTempMsg(notice.getUserBaseInfo()));
        noticeMsg.setTime(notice.getTime());
        noticeMsg.setType(notice.getType());
        noticeMsg.setReward(notice.getReward());
        return noticeMsg;
    }
}
